package com.huluobo.a2110mvpimdemo.demo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;
import android.widget.ImageView;

import com.huluobo.a2110mvpimdemo.R;

/**
 * 动画:
 * 补间动画:演示效果,平移(translate),缩放(scale),旋转(rotate),头闽都(alpha),还需要设置起始值和终点值即可,系统会自动补充其间的动画
 * 帧动画:一帧一帧的动画,需要大量图片资源才好看,一般不推荐使用,因为大量突破会影响apk包的体积
 * 属性动画:与补间动画很像,因为4中动画基本相同,但是实际改变了imageView或者View的属性,补间动画的缺点在于动画过后,实际的view位置或者属性没有改变
 * 但是属性动画会改变实际view的值
 * ValueAnimator和ObjectAnimator
 * 区别:ValueAnimator需要写监听器,并且在监听器中设置动画效果,如果不设置,则没有任何变化
 * ObjectAnimator,不需要写监听器,直接在实例化的时候把需要改变的view,动画效果,和值一起写入到参数中
 */
public class AnimActivity extends AppCompatActivity {
    private ImageView imageView;
    private Button translate, rotateBtn, scaleBtn, alphaBtn, setBtn, valueAnimatorBtn, objectAnimatorBtn,
            animatorSetBtn, interpolatorBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anim);

        imageView = findViewById(R.id.imageView);
        translate = findViewById(R.id.translate);
        rotateBtn = findViewById(R.id.rotate_btn);
        scaleBtn = findViewById(R.id.scale_btn);
        alphaBtn = findViewById(R.id.alpha_btn);
        setBtn = findViewById(R.id.set_btn);
//        progressIv = findViewById(R.id.progress_iv);
//        progressBtn=findViewById(R.id.progress_btn);
        valueAnimatorBtn = findViewById(R.id.valueAnimatorBtn);
        objectAnimatorBtn = findViewById(R.id.objectAnimatorBtn);
        animatorSetBtn = findViewById(R.id.animatorSet);
        interpolatorBtn = findViewById(R.id.interpolator);

        Animation animationTranslate = AnimationUtils.loadAnimation(this, R.anim.anim_translate);
        translate.setOnClickListener(v -> {
            imageView.startAnimation(animationTranslate);
        });

        Animation animationRotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
        rotateBtn.setOnClickListener(v -> {
            imageView.startAnimation(animationRotate);
        });

        Animation animationScale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
        scaleBtn.setOnClickListener(v -> {
            imageView.startAnimation(animationScale);
        });

        Animation animationAlpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
        alphaBtn.setOnClickListener(v -> {
            imageView.startAnimation(animationAlpha);
        });

        Animation animationSet = AnimationUtils.loadAnimation(this, R.anim.anim_set);
        animationSet.setRepeatCount(-1);//重复次数,-1代表无限重复
        animationSet.setRepeatMode(Animation.REVERSE);//重复模式 REVERSE 颠倒 RESTART 重新开始
        setBtn.setOnClickListener(v -> {
            imageView.startAnimation(animationSet);
        });

//        AnimationDrawable animationDrawable = (AnimationDrawable) progressIv.getDrawable();
//        progressBtn.setOnClickListener(v -> {
//            animationDrawable.start();
//        });
        //属性动画
        valueAnimatorBtn.setOnClickListener(v -> {
            ValueAnimator valueAnimator = ValueAnimator.ofFloat(1f, 0.1f, 1f, 0.5f, 1f, 0f);
            valueAnimator.setDuration(5000);
            valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
            valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
            //添加一个监听器,可以监听动画的值的变化
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                    //currentValue 就是当前属性动画中变化的值
                    float currentValue = (float) animation.getAnimatedValue();
                    Log.i("TagA", "currentValue:" + currentValue);
                    //把这个值给到imageView,设置透明度
                    imageView.setAlpha(currentValue);
                    imageView.setScaleX(currentValue);
                    imageView.setScaleY(currentValue);
                }
            });
            valueAnimator.start();
        });

        objectAnimatorBtn.setOnClickListener(v -> {
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.1f, 1f, 0.5f, 1f);
            objectAnimator.setDuration(5000);
            objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
            objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
            objectAnimator.start();
        });

        animatorSetBtn.setOnClickListener(v -> {
            ObjectAnimator rotate = ObjectAnimator.ofFloat(imageView, "rotation", 0, 360);
            ObjectAnimator translateX = ObjectAnimator.ofFloat(imageView, "translationX", -100, 100);
            ObjectAnimator translateY = ObjectAnimator.ofFloat(imageView, "translationY", -100, 100);
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1, 2);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1, 2);
            ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1, 0, 1);

            //1.拉长Y 2.旋转并且透明 并且拉长X 3 平移
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.play(rotate)//要执行的效果,必须要有个play
                    .with(alpha)//与play同时执行
                    .with(scaleX)//与play同时执行
                    .after(scaleY) //1 执行完 再执行play
                    .after(1000)// 将play延迟1秒后执行,也就是执行完1秒再执行play
                    .before(translateX)//执行完play,再执行
                    .before(translateY);

            animatorSet.setDuration(15000);
            animatorSet.start();
        });

        interpolatorBtn.setOnClickListener(v -> {
            ObjectAnimator translateY = ObjectAnimator.ofFloat(imageView, "rotation", 0 ,360);
//            translateY.setInterpolator(new AccelerateInterpolator());//加速度插值器,开始慢 中间开始加速
//            translateY.setInterpolator(new DecelerateInterpolator());//减速插值器,开始快 中间开始减速
//            translateY.setInterpolator(new AccelerateDecelerateInterpolator());//先加速,后减速,开始慢,中间快,结束慢
//            translateY.setInterpolator(new AnticipateInterpolator());//反向加速,先后退一段距离,然后再加速
            translateY.setInterpolator(new BounceInterpolator());//弹球落地效果
//            translateY.setInterpolator(new CycleInterpolator(2));//循环插值器
//            translateY.setInterpolator(new LinearInterpolator());//线性插值器,匀速运动,默认
//            translateY.setInterpolator(new OvershootInterpolator());//超越插值器,超过终点一段距离,再返回终点
            translateY.setDuration(2000);
            translateY.setRepeatCount(-1);
            translateY.setRepeatMode(ValueAnimator.RESTART);
            translateY.start();
        });
    }
}